﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace YTSK
{
    public static class DictionaryExt
    {
        public static T ToObj<T>(this Dictionary<string, object> dic, bool toLocal = false)
        {
            var obj = Activator.CreateInstance<T>();
            Type t = obj.GetType();
            PropertyInfo[] ps = t.GetProperties();
            foreach (PropertyInfo p in ps)
            {
                string pname = p.Name;
                if (dic.ContainsKey(pname))
                {
                    var val = dic[pname];
                    if (val == DBNull.Value) { val = null; }
                    if (val.ObjToString().Length == 0) { val = null; }
                    var bset = false;
                    if (val != null)
                    {
                        if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(decimal?))
                        { p.SetValue(obj, val.ObjToDecimal(), null); bset = true; }
                        if (p.PropertyType == typeof(int) || p.PropertyType == typeof(int?))
                        { p.SetValue(obj, val.ObjToInt(), null); bset = true; }
                        if (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?))
                        { p.SetValue(obj, val.ObjToDate(), null); bset = true; }
                        if (p.PropertyType == typeof(bool))
                        { p.SetValue(obj, val.ObjToBool(), null); bset = true; }
                        if(p.PropertyType == typeof(bool?))
                        {
                            if (val == null) { p.SetValue(obj, null, null); }
                            else { p.SetValue(obj, val.ObjToBool(), null); }
                            bset = true;
                        }
                        if (p.PropertyType == typeof(byte[]))
                        {
                            //var tmp = Convert.ToByte(val);
                            //p.SetValue(obj, tmp, null);
                            bset = true;
                        }
                    }
                    if (!bset)
                    { p.SetValue(obj, val, null); }
                }
            }
            return obj;
        }

        public static List<T> ToList<T>(this List<Dictionary<string, object>> list)
        {
            var ret_list = new List<T>();
            for (int i = 0; i < list.Count; i++)
            {
                var dic = list[i];
                var obj = dic.ToObj<T>();
                ret_list.Add(obj);
            }
            return ret_list;
        }


        public static object getValue(this Dictionary<string, object> dic, string key)
        {
            object obj = null;
            if (dic.ContainsKey(key))
            {
                obj = dic[key];
            }
            return obj;
        }

        public static string ToUrl(this Dictionary<string, object> dic)
        {
            return Common.handleJSON.Serialize(dic);
            //string buff = "";
            //foreach (var key in dic.Keys)
            //{
            //    buff += key + "=" +  dic[key].ObjToString() + "&";
            //}
            //buff = buff.Trim('&');
            //return buff;
        }
    }
}
